package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.CustFlowEventRepository;
import com.realtyManage.springboot.Model.Do.CustFlowEvent;
import com.realtyManage.springboot.Model.Do.Customer;
import com.realtyManage.springboot.Model.Do.User;
import com.realtyManage.springboot.Model.Vo.CustFlowEntity;
import com.realtyManage.springboot.Service.ICustFlowEventService;
import com.realtyManage.springboot.Service.ICustomerService;
import com.realtyManage.springboot.Service.IIndexService;
import com.realtyManage.springboot.Service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * CustFlowEventServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/3/22
 **/
@Service
@Transactional
public class CustFlowEventServiceImpl implements ICustFlowEventService {
    @Autowired
    private CustFlowEventRepository custFlowEventRepository;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IIndexService indexService;

    @Override
    public List<CustFlowEntity> getFlowEventListByCid(Integer cid, User currentUser) {
        List<CustFlowEntity> list = new ArrayList<CustFlowEntity>();
        List<CustFlowEvent> custFlowEntityList = custFlowEventRepository.findByConnCustIdOrderByOperateCodeAsc(cid);
        Customer customer = customerService.getCustomerDetail(cid);
        //HouseDetail houseDetail = indexService.getHouseDetail(customer.getConnHouseId());
        User user = userService.findById(customer.getConnUserId());

        Double shopChargeRate = (customer.getShopChargeRate()!=null?customer.getShopChargeRate()/100:0.00);
        Double shopChargeFixed = (customer.getShopChargeFixed()!=null?customer.getShopChargeFixed():0.00);
        String shopChargeExtra = (customer.getShopChargeExtra()!=null?customer.getShopChargeExtra():"");

        Double personChargeRate = (customer.getPersonChargeRate()!=null?customer.getPersonChargeRate()/100:0.00);
        Double personChargeFixed = (customer.getPersonChargeFixed()!=null?customer.getPersonChargeFixed():0.00);
        String personChargeExtra = (customer.getPersonChargeExtra()!=null?customer.getPersonChargeExtra():"");

        Double indChargeRate = (customer.getIndChargeRate()!=null?customer.getIndChargeRate()/100:0.00);
        Double indChargeFixed = (customer.getIndChargeFixed()!=null?customer.getIndChargeFixed():0.00);
        String indChargeExtra = (customer.getIndChargeExtra()!=null?customer.getIndChargeExtra():"");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(CustFlowEvent custFlowEvent : custFlowEntityList){
            CustFlowEntity entity = new CustFlowEntity();
            entity.setOperateCode(custFlowEvent.getOperateCode());
            entity.setTime(custFlowEvent.getTime());
            entity.setHandleName(userService.findById(custFlowEvent.getHandleUserId()).getName());
            entity.setRemark(custFlowEvent.getRemark());
            switch (custFlowEvent.getOperateCode()){
                case 2://已到访
                    break;
                case 3://已认购
                    entity.setBuyMoney(custFlowEvent.getBuyMoney());
                    entity.setExpectSignDate(custFlowEvent.getExpectSignDate());

                    Double money = 0.00;
                    String expectCharge = "";
                    if(customer.getConnUserId() != currentUser.getId()){//当前查看人为上线
                        if(currentUser.getLevel()==4){//门店负责人
                            money = custFlowEvent.getBuyMoney()*shopChargeRate/100+shopChargeFixed;
                            expectCharge += money.toString() + "  " + shopChargeExtra;
                        }else if(currentUser.getLevel()==3){//门店经纪人显示的是提成
                            money = (custFlowEvent.getBuyMoney()*indChargeRate/100+indChargeFixed)*customer.getChargeRate()/100;
                            expectCharge += money.toString();
                        }else if(currentUser.getLevel()==2){//独立经纪人显示的是提成
                            money = (custFlowEvent.getBuyMoney()*indChargeRate/100+indChargeFixed)*customer.getChargeRate()/100;
                            expectCharge += money.toString();
                        }
                        //TODO 对于管理员的显示
                    }else{//当前查看人为成交用户
                        if(user.getLevel()==4){//门店负责人
                            money = custFlowEvent.getBuyMoney()*shopChargeRate/100+shopChargeFixed;
                            expectCharge += money.toString() + "  " + shopChargeExtra;
                        }else if(user.getLevel()==3){//门店经纪人
                            money = custFlowEvent.getBuyMoney()*personChargeRate/100+personChargeFixed;
                            expectCharge += money.toString() + "  " + personChargeExtra;
                        }else if(user.getLevel()==2){//独立经纪人
                            money = custFlowEvent.getBuyMoney()*indChargeRate/100+indChargeFixed;
                            expectCharge += money.toString() + "  " + indChargeExtra;
                        }
                    }

                    entity.setExpectCharge(expectCharge);
                    entity.setEarnestMoney(custFlowEvent.getEarnestMoney());
                    entity.setSubscriptionBook(custFlowEvent.getSubscriptionBook());
                    break;
                case 4: //已签约
                    entity.setRealMoney(custFlowEvent.getRealMoney());
                    entity.setCommissionTime(custFlowEvent.getCommissionTime());

                    Double money1 = 0.00;
                    String realCharge = "";

                    if(customer.getConnUserId() != currentUser.getId()){//当前查看人为上线
                        if(currentUser.getLevel()==4){//门店负责人
                            money = custFlowEvent.getRealMoney()*shopChargeRate/100+shopChargeFixed;
                            realCharge += money.toString() + "  " + shopChargeExtra;
                        }else if(currentUser.getLevel()==3){//门店经纪人显示的是提成
                            money = (custFlowEvent.getRealMoney()*indChargeRate/100+indChargeFixed)*customer.getChargeRate()/100;
                            realCharge += money.toString();
                        }else if(currentUser.getLevel()==2){//独立经纪人显示的是提成
                            money = (custFlowEvent.getRealMoney()*indChargeRate/100+indChargeFixed)*customer.getChargeRate()/100;
                            realCharge += money.toString();
                        }
                        //TODO 对于管理员的显示
                    }else{//当前查看人为成交用户
                        if(user.getLevel()==4){//门店负责人
                            money = custFlowEvent.getRealMoney()*shopChargeRate/100+shopChargeFixed;
                            realCharge += money.toString() + "  " + shopChargeExtra;
                        }else if(user.getLevel()==3){//门店经纪人
                            money = custFlowEvent.getRealMoney()*personChargeRate/100+personChargeFixed;
                            realCharge += money.toString() + "  " + personChargeExtra;
                        }else if(user.getLevel()==2){//独立经纪人
                            money = custFlowEvent.getRealMoney()*indChargeRate/100+indChargeFixed;
                            realCharge += money.toString() + "  " + indChargeExtra;
                        }
                    }

                    entity.setRealCharge(realCharge);
                    entity.setFirstMoney(custFlowEvent.getFirstMoney());
                    entity.setContract(custFlowEvent.getContract());
                    break;
                case 5://已退房
                    entity.setRemark(custFlowEvent.getRemark());
                    entity.setReturnPic(custFlowEvent.getReturnPic());
                    break;
                case 6://无效
                    break;
                case 7://已结佣
                    entity.setRealMoney(custFlowEvent.getRealMoney());
                    entity.setCommissionPic(custFlowEvent.getCommissionPic());
                    break;
                default:
                    break;
            }
            list.add(entity);
        }
        return list;
    }

    @Override
    public CustFlowEvent findByConnCustIdAndOperateCode(Integer connCustId, int operateCode) {
        return custFlowEventRepository.findByConnCustIdAndOperateCode(connCustId,operateCode);
    }
}
